package com.teamall.web.app.controller;

import com.jcraft.jsch.jce.MD5;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.util.BeanUtils;
import com.teamall.framework.util.IdCreateUtils;
import com.teamall.framework.util.SpringAppContextUtil;
import com.teamall.framework.util.Utils;
import com.teamall.web.app.vo.AppUserVO;
import com.teamall.web.base.vo.ResResult;
import com.teamall.web.sys.db.po.SysUserPO;
import com.teamall.web.sys.db.po.SysUserTokenPO;
import com.teamall.web.sys.service.ISysUserService;
import com.teamall.web.sys.service.impl.SysUserTokenServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author
 * @create 2017-07-01 21:37
 **/
@Controller
@RequestMapping("/app/user")
public class AppUserController {
    @Autowired
    private ISysUserService sysUserService;

    @RequestMapping("/login")
    @ResponseBody
    public ResResult login(String userName, String password, Integer userObjType) throws BaseAppException {
        ResResult resResult = new ResResult();
        if (Utils.isEmpty(userName) || Utils.isEmpty(password) || Utils.isEmpty(userObjType)) {
            resResult.setCode("9002");
            resResult.setMsg("登陆参数有错");
            return resResult;
        }
        AppUserVO appUserVO = sysUserService.userLogin(userName, password, userObjType);
        if (Utils.isEmpty(appUserVO)) {
            resResult.setCode("9002");
            resResult.setMsg("用户密码错误");
        } else {
            resResult.setRspData(appUserVO);
        }
        return resResult;
    }

    @RequestMapping("/password")
    @ResponseBody
    public ResResult password(HttpServletRequest request, String oldPassword, String newPassword) throws BaseAppException {
        ResResult resResult = new ResResult();
        if ( Utils.isEmpty(oldPassword) || Utils.isEmpty(newPassword)) {
            resResult.setCode("7000");
            resResult.setMsg("参数错误");
            return resResult;
        }
        String oldToken = request.getHeader("token");
        Integer userId = this.getUserByToken(oldToken).getUserId();
        String token = sysUserService.updPassword(userId, oldPassword, newPassword);
        if (token.equals("-1")) {
            resResult.setCode("9002");
            resResult.setMsg("用户不存在");
            return resResult;
        } else if (token.equals("-2")) {
            resResult.setCode("9002");
            resResult.setMsg("旧密码不正确");
            return resResult;
        } else {
            Map result = new HashMap();
            result.put("token", token);
            resResult.setRspData(result);
        }
        return resResult;
    }

    @RequestMapping("/passwordByName")
    @ResponseBody
    public ResResult password(String loginName, String oldPassword, Integer userObjType, String newPassword) throws BaseAppException {
        ResResult resResult = new ResResult();
        if (Utils.isEmpty(loginName) || Utils.isEmpty(userObjType) || Utils.isEmpty(oldPassword) || Utils.isEmpty(newPassword)) {
            resResult.setCode("7000");
            resResult.setMsg("参数错误");
            return resResult;
        }
        String token = sysUserService.updPassword(loginName, oldPassword, userObjType, newPassword);
        if (token.equals("-1")) {
            resResult.setCode("9002");
            resResult.setMsg("用户不存在");
            return resResult;
        } else if (token.equals("-2")) {
            resResult.setCode("9002");
            resResult.setMsg("旧密码不正确");
            return resResult;
        } else {
            Map result = new HashMap();
            result.put("token", token);
            resResult.setRspData(result);
        }
        return resResult;
    }
    public SysUserTokenPO getUserByToken(String token) throws BaseAppException {
        SysUserTokenServiceImpl sysUserTokenService = SpringAppContextUtil.getBean(SysUserTokenServiceImpl.class);
        SysUserTokenPO sysUserTokenPO = sysUserTokenService.selectByToken(token);
        return sysUserTokenPO;
    }
}
